package com.game.modular.game.battle.process.battleStage.actionPhase.actionPhase2.item;

import cn.hutool.core.util.RandomUtil;
import com.game.modular.game.battle.panel.Battle;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.game.modular.game.battle.panel.skillEffect.domain.*;
import com.game.modular.game.battle.panel.attribute.PlayerHeroAttributeServiceImpl;
import com.game.modular.game.playerAssets.domain.PlayerHero;
import com.game.modular.gameCore.battle.domain.CoreSkillEffect;
import com.game.modular.gameCore.battle.domain.CoreSkillEffectType;
import com.game.modular.gameCore.battle.service.impl.CoreSkillEffectInfoServiceImpl;
import com.game.modular.gameCore.cultivate.domain.CoreSkill;
import com.game.modular.gameCore.cultivate.domain.CoreSkillStar;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 4-3
 * 获取技能效果列表
 */
@Slf4j
@Service
@Component
public class ReleaseSkillService3 {
    @Resource
    private CoreSkillEffectInfoServiceImpl coreSkillEffectTypeServiceImpl;
    @Resource
    private PlayerHeroAttributeServiceImpl playerHeroAttributeServiceImpl;

    /**
     * 技能效果抽取|处理所有目标获取的技能效果信息
     */
    public List<PvpSkillEffect> obtainSkillEffects(
            @NotNull(message = "战斗id不能为空") Long battleId,
            CoreSkill coreSkill, CoreSkillStar coreSkillStar,
            @NotNull(message = "施法者不能为空") PlayerHero actWJ,
            @NotNull(message = "目标武将不能为空") List<PlayerHero> targetList,
            @NotNull(message = "技能效果不能为空") CoreSkillEffect coreSkillEffect
    ) {
        coreSkillEffect.obtainNumericalValues(coreSkillStar);
        Set<Long> coreSkillEffectTypeIdList = obtainCoreSkillEffectTypeId(coreSkillEffect);
        return handTargetStatusMessage(
                battleId, coreSkill, coreSkillStar, actWJ, targetList, coreSkillEffect, coreSkillEffectTypeIdList
        );
    }


    /**
     * 1 技能效果抽取
     */
    public Set<Long> obtainCoreSkillEffectTypeId(CoreSkillEffect coreSkillEffect) {
        int numToReturn = coreSkillEffect.getEffectTypeNum();
        Set<Long> coreSkillEffectTypeId = coreSkillEffect.getEffectTypeId();

        if (numToReturn >= coreSkillEffectTypeId.size()) {
            return coreSkillEffectTypeId;
        }

        try {
            return RandomUtil.randomEleSet(coreSkillEffectTypeId, numToReturn);
        } catch (Exception e) {
            // 异常处理，例如记录日志或抛出自定义异常
            throw new RuntimeException("Failed to select random elements", e);
        }
    }

    /**
     * 2 处理所有目标获取的技能效果信息
     */
    private List<PvpSkillEffect> handTargetStatusMessage(
            Long battleId,
            CoreSkill coreSkill, CoreSkillStar coreSkillStar,
            PlayerHero actWJ, List<PlayerHero> targetList,
            CoreSkillEffect coreSkillEffect,
            Set<Long> coreSkillEffectTypeIdList
    ) {
        Battle battle = Battle.get(battleId);
        Integer currentNumberOfRounds = battle.getRound();


        List<PvpSkillEffect> list = new ArrayList<>();
        for (Long coreSkillEffectTypeId : coreSkillEffectTypeIdList) {
            CoreSkillEffectType coreSkillEffectType =
                    coreSkillEffectTypeServiceImpl.getCache(coreSkillEffectTypeId);


            list.addAll(targetList.stream()
                    .map(target -> {
                        PvpSkillEffect.PvpSkillEffectBuilder builder = PvpSkillEffect.builder();
                        ///////////////////////////////////////////////////////////////////////////////////////  基础部分
                        builder.battleId(battleId)
                                .round(currentNumberOfRounds)
                                .sourcePlayerHeroId(actWJ.getId())
                                .sourcePlayerHeroName(actWJ.getName())
                                .targetPlayerHeroId(target.getId())
                                .targetPlayerHeroName(target.getName())
                        ;
                        ///////////////////////////////////////////////////////////////////////////////////////  技能信息
                        builder.sourceCoreSkillId(coreSkill.getId())
                                .coreSkillTyp(coreSkill.getType())
                                .isMagic(coreSkill.getIsMagic() == null ? 0 : coreSkill.getIsMagic())
                                .magicInfluence(coreSkill.getMagicInfluence() == null ? 0 : coreSkill.getMagicInfluence())
                        ;
                        ///////////////////////////////////////////////////////////////////////////////////// 技能新阶信息
                        builder.sourceCoreSkillStarId(coreSkillStar.getId())
                                .build();
                        ///////////////////////////////////////////////////////////////////////////////////// 技能效果类型
                        builder.coreSkillEffectTypeId(coreSkillEffectType.getId())
                                .coreSkillEffectTypeName(coreSkillEffectType.getName())
                                .coreSkillEffectTypeType(coreSkillEffectType.getBuffType())
                                .immunityList(coreSkillEffectType.getImmuneList())
                                .removeList(coreSkillEffectType.getRemoveList())
                        ;
                        ////////////////////////////////////////////////////////////////////////////////////  技能数值部分
                        builder
                                .coreSkillEffectId(coreSkillEffect.getId())
                                .baseTrigger(BaseTrigger.builder()
                                        .trigger1(coreSkillEffect.getTrigger1())
                                        .trigger2(coreSkillEffect.getTrigger2())
                                        .trigger3(coreSkillEffect.getTrigger3())
                                        .trigger4(coreSkillEffect.getTrigger4())
                                        .trigger5(coreSkillEffect.getTrigger5())
                                        .triggerNum(coreSkillEffect.getTriggerNum())
                                        .appoint(coreSkillEffect.getAppoint())
                                        .prepare(coreSkillEffect.getPrepare())
                                        .resProbability(coreSkillEffect.getResProbability())
                                        .keep(coreSkillEffect.getKeep())
                                        .build()
                                )
                                .baseEffectValueInfo(BaseEffectValueInfo.builder()
                                        .isBenefit(coreSkillEffectType.getIsBenefit())
                                        .resDoubleValue1(coreSkillEffect.getResDoubleValue1())
                                        .resIntegerValue1(coreSkillEffect.getResIntegerValue1())
                                        .resDoubleValue2(coreSkillEffect.getResDoubleValue2())
                                        .resIntegerValue2(coreSkillEffect.getResIntegerValue2())
                                        .layerMax(coreSkillEffect.getBaseValueMax())
                                        .layerMax(coreSkillEffect.getLayerMax())
                                        .baseValue(
                                                playerHeroAttributeServiceImpl.getBaseValue(
                                                        battleId,
                                                        target.getId(),
                                                        coreSkillEffectType.getEffectAttribute()
                                                ))
                                        .build()
                                )
                                .skillEffectLayer(SkillEffectLayer.builder()
                                        .name(coreSkillEffectType.getEffectAttribute())
                                        .triggerCount(0)
                                        .info("等待获取")
                                        .layerValueMap(new HashMap<>())
                                        .thisChangeValue(0.0)
                                        .totalChangeValue(0.0)
                                        .build()
                                )
                                .status(SkillEffectStatus.builder()
                                        .isTrigger(1)
                                        .isSave(1)
                                        .triggerFailReasons(new ArrayList<>())
                                        .status(1)
                                        .failReasons(new ArrayList<>())
                                        .numberOfTriggers(0)
                                        .build()
                                )
                        ;
                        return builder.build();
                    })
                    .collect(Collectors.toList()));
        }
        return list;
    }
}
